package com.Policing.Receive;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.Policing.Adapter.Receive_da;
import com.Policing.R;
import com.Policing.CanniBalize.Shift_site;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/16.
 */

public class add_category extends Activity implements OnDismissListener {
    private ListView add_lv;
    private LinearLayout property,position;//资产名称，使用位置
    private TextView property_tv,position_tv,user;
    private EditText add_number;
    private Button upload,add;
    List<Map<String, Object>> lv_list = new ArrayList<Map<String, Object>>();
    private List<String> propertyids = new ArrayList<>();
    private List<String> site_ids = new ArrayList<>();
    private List<String> userids = new ArrayList<>();
    private String site_id="",property_num="";
    private ImageView fh;
    private String propertyid="";
    private String userid= "";
    private TextView title_text;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_consumable);
        //url = "http://192.168.1.159:8087/api/getAllPut";
        inview();
        property.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(add_category.this, Receive_property.class);
                startActivityForResult(intent,3);
            }
        });
        position.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(add_category.this, Shift_site.class);
                startActivityForResult(intent,2);
            }
        });
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONArray array =new JSONArray();
                JSONArray jsonArray = new JSONArray();
                for (int i =0;i<lv_list.size();i++){
                    JSONObject object =new JSONObject();
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("type",propertyids.get(i));
                        jsonObject.put("num",lv_list.get(i).get("receivesDept_name"));
                        jsonObject.put("user",userids.get(i));
                        jsonObject.put("place",site_ids.get(i));

                        object.put("name",lv_list.get(i).get("coding_name"));
                        object.put("num",lv_list.get(i).get("receivesDept_name"));
                        object.put("user",lv_list.get(i).get("receivesUser_name"));
                        object.put("place",lv_list.get(i).get("reapplyDate_name"));
                        jsonArray.put(jsonObject);
                        array.put(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("data",jsonArray.toString());
                intent.putExtra("list",array.toString());
                add_category.this.setResult(6,intent);
                add_category.this.finish();//把item的内容返回给之前的activity
            }
        });
        add_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int i, long id) {
                new AlertView("提示", "是否删除", "取消",new String[]{"确定"}, null, add_category.this, AlertView.Style.Alert, new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        //取消：position=-1,确定 position=0
                        if (position==0){
                            lv_list.remove(i);
                            propertyids.remove(i);
                            site_ids.remove(i);
                            userids.remove(i);
                            Receive_da receive_da = new Receive_da(add_category.this,lv_list);
                            add_lv.setAdapter(receive_da);
                        }

                    }
                }).setCancelable(true).setOnDismissListener(add_category.this).show();
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (property_num.equals("")){
                    Toast.makeText(add_category.this,"请选择选择资产类型后重试！",Toast.LENGTH_LONG).show();
                    return;
                }
                int i = Integer.parseInt(property_num);
                int j = Integer.parseInt(add_number.getText().toString());
                if (property_tv.getText().toString().equals("")||user.getText().toString().equals("")||site_id.equals("")||add_number.getText().toString().equals("")){
                    Toast.makeText(add_category.this,"请完善数据后重试！",Toast.LENGTH_LONG).show();
                    return;
                }
                if (j>i){
                    Toast.makeText(add_category.this,"数量不能大于库存数量！此时库存数量为："+property_num,Toast.LENGTH_LONG).show();
                    return;
                }
                if (j==0){  Toast.makeText(add_category.this,"数量不能为0",Toast.LENGTH_LONG).show();
                    return;}
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("coding_name",property_tv.getText().toString().trim());
                map.put("receivesDept_name",add_number.getText().toString());
                map.put("receivesUser_name",user.getText().toString());
                map.put("reapplyDate_name",position_tv.getText().toString());
                propertyids.add(propertyid);
                site_ids.add(site_id);
                lv_list.add(map);
                userids.add(userid);
                Receive_da receive_da = new Receive_da(add_category.this,lv_list);
                add_lv.setAdapter(receive_da);
                property_tv.setText("");
                position_tv.setText("");
                site_id="";
                propertyid ="";
                userid="";
                property_num="";
                add_number.setText("");
                user.setText("");
            }
        });
    }

    private void inview() {
        user = (TextView) findViewById(R.id.user);
        position_tv = (TextView) findViewById(R.id.position_tv);
        position = (LinearLayout) findViewById(R.id.position);
        add_number = (EditText) findViewById(R.id.num);
        property_tv = (TextView) findViewById(R.id.property_tv);
        property = (LinearLayout) findViewById(R.id.property);
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("添加资产");
        fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        upload = (Button) findViewById(R.id.upload);
        add_lv = (ListView) findViewById(R.id.add_lv);
        add = (Button) findViewById(R.id.add);
    }

    @Override
    public void onDismiss(Object o) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==2&&resultCode==3){
            position_tv.setText(data.getExtras().getString("site_name"));
            site_id = data.getExtras().getString("site_id");
        }
        if (requestCode==3&&resultCode==4){
            property_tv.setText(data.getExtras().getString("name"));
            property_num = data.getExtras().getString("num");
            propertyid = data.getExtras().getString("pid");
            add_number.setText(property_num);
        }
        if (requestCode==7&&resultCode==7){
            user.setText(data.getExtras().getString("admin_name"));
            userid = data.getExtras().getString("admin_id");
        }
    }
    public void User(View view){
        Intent intent = new Intent(add_category.this, add_admin.class);
        startActivityForResult(intent,7);
    }
}
